package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.ContactDao;
import cz.khfotbal.domain.Contact;

@Repository
@Transactional
public class ContactDaoImpl implements ContactDao{

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;
	
	public List<Contact> findAllContacts(String category) {
		session = sessionFactory.getCurrentSession();
		List<Contact> list = session.createQuery("from Contact where category = '"+category+"'").list();
		return list;
	}

	public void saveContact(Contact contact) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(contact);
	}

	public void updateContact(Contact contact) {
		session = sessionFactory.getCurrentSession();
		session.merge(contact);
	}

	public Contact findContactById(int id) {
		session = sessionFactory.getCurrentSession();
		Contact contact = (Contact) session.createQuery("from Contact where id ="+id).uniqueResult();
		return contact;
	}

	public void deleteContact(Contact contact) {
		session = sessionFactory.getCurrentSession();
		session.delete(contact);
	}

}
